package com.jay.tools.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Html;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jay.tools.R;


/**
 * description: 顶部导航栏
 * author: tjp
 * date: 2017/7/20 020 14:07
 */
public class TopBarView extends RelativeLayout {

    public interface topBarClickListener {
        void leftClick();

        void rightClick();
    }

    private topBarClickListener mListener;

    public void setOnTopBarClickListener(topBarClickListener listener) {
        mListener = listener;
    }

    public static class OnTopBarClick implements topBarClickListener {

        @Override
        public void leftClick() {

        }

        @Override
        public void rightClick() {

        }

    }

    private int mDefTextColor = 0xFFFFFFFF;
    private int mDefBackGround = 0xFF00FF00;

    private int mHeight = dp2px(48);

    // 左侧
    private TextView mLeftTextView;
    private String mLeftStr;
    private int mLeftTextSize = sp2px(16);
    // 中间标题
    private TextView mTitleTextView;
    private String mTitleStr;
    private int mTitleTextSize = sp2px(16);
    // 右侧
    private TextView mRightTextView;
    private String mRightStr;
    private int mRightTextSize = sp2px(16);
    ;

    public TextView getmLeftTextView() {
        return mLeftTextView;
    }

    public TopBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        obtainAttrs(context, attrs);
    }

    private void obtainAttrs(Context context, AttributeSet attrs) {
        initObtain(context, attrs);
        View v = LayoutInflater.from(context).inflate(R.layout.ui_layout_topbar_view, this, true);

        View item = v.findViewById(R.id.rl_top_bar);
        ViewGroup.LayoutParams layoutParams = item.getLayoutParams();
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = mHeight;
        item.setLayoutParams(layoutParams);

        item.setBackgroundColor(mDefBackGround);

        initLeft(v);
        initTitle(v);
        initRight(v);
    }

    private void initLeft(View v) {
        if (TextUtils.isEmpty(mLeftStr)) {
            return;
        }
        mLeftTextView = v.findViewById(R.id.tv_topbar_left);
        mLeftTextView.setText(mLeftStr);
        mLeftTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mLeftTextSize);
        mLeftTextView.setTextColor(mDefTextColor);
        mLeftTextView.setVisibility(View.VISIBLE);

        mLeftTextView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.leftClick();
                }
            }
        });
    }

    private void initRight(View v) {
        if (TextUtils.isEmpty(mRightStr)) {
            return;
        }
        mRightTextView = v.findViewById(R.id.tv_topbar_right);
        mRightTextView.setText(mRightStr);
        mRightTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mRightTextSize);
        mRightTextView.setTextColor(mDefTextColor);
        mRightTextView.setVisibility(View.VISIBLE);
        mRightTextView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.rightClick();
                }
            }
        });
    }

    private void initTitle(View v) {
        mTitleTextView = v.findViewById(R.id.tv_topbar_title);
        mTitleTextView.setText(TextUtils.isEmpty(mTitleStr) ? "" : Html.fromHtml(mTitleStr));
        mTitleTextView.setTextColor(mDefTextColor);
        mTitleTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTitleTextSize);
        mTitleTextView.setVisibility(View.VISIBLE);
    }

    public void setTitle(String str) {
        if (TextUtils.isEmpty(str)) {
            return;
        }
        mTitleStr = str;
        mTitleTextView.setText(Html.fromHtml(mTitleStr));
    }

    private void initObtain(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TopBarView);
        mDefBackGround = a.getColor(R.styleable.TopBarView_backgroud, mDefBackGround);
        mDefTextColor = a.getColor(R.styleable.TopBarView_text_color, mDefTextColor);

        mLeftStr = a.getString(R.styleable.TopBarView_left_text);
        mTitleStr = a.getString(R.styleable.TopBarView_title_text);
        mRightStr = a.getString(R.styleable.TopBarView_right_text);

        mLeftTextSize = a.getDimensionPixelSize(R.styleable.TopBarView_left_text_size, mLeftTextSize);
        mTitleTextSize = a.getDimensionPixelSize(R.styleable.TopBarView_title_text_size, mTitleTextSize);
        mRightTextSize = a.getDimensionPixelSize(R.styleable.TopBarView_right_text_size, mRightTextSize);
        mHeight = a.getDimensionPixelSize(R.styleable.TopBarView_height, mHeight);
        a.recycle();
    }

    private int sp2px(float sp) {
        return (int) (sp * getContext().getResources().getDisplayMetrics().scaledDensity + 0.5);
    }

    private int dp2px(float dp) {
        return (int) (dp * getContext().getResources().getDisplayMetrics().density + 0.5f);
    }

}
